package com.itheima.reggin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggin.common.CustomException;
import com.itheima.reggin.dto.SetmealDto;
import com.itheima.reggin.entity.Setmeal;
import com.itheima.reggin.entity.SetmealDish;
import com.itheima.reggin.mapper.SetmealMapper;
import com.itheima.reggin.service.SetmealDishService;
import com.itheima.reggin.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;

    /**
     * 新增套餐的保存方法
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);//因为是继承的所以可以直接保存
        //保存菜品表
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //获取套餐的id
        Long id = setmealDto.getId();
        list.stream().map((item) -> {
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeWithDish(List<Long> ids) {
        /**
         * select count(*) from setmeal where id id (1,2,,3) and status=1
         * 删除套餐需要删除两张表里面的内容
         */
        LambdaQueryWrapper<Setmeal> querys = new LambdaQueryWrapper<>();
        querys.eq(Setmeal::getStatus, 1);
        querys.in(Setmeal::getId, ids);
        int count = this.count(querys);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中 不能删除！");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> query = new LambdaQueryWrapper<>();
        query.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(query);
    }

    /**
     * 查询套餐信息
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SetmealDto selectWithDishs(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> query = new LambdaQueryWrapper<>();
        query.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(query);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    /**
     * 修改套餐的保存方法,保存两张表，先删除 在保存
     * * @param setmealDto
     */
    @Override
    public void saveWithDishById(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        Long id = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> query = new LambdaQueryWrapper<>();
        query.eq(SetmealDish::getSetmealId, id);
        setmealDishService.remove(query);
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list.stream().map((item) -> {
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }
}
